import { InjectRepository } from "@nestjs/typeorm";
import { UserRepositoryAbstract } from "../user-abstract.repository";
import { User } from "../entities/user.entity";
import { Repository } from "typeorm";
import { TYPEORM_DATABASE } from "@/database/database.constants";

export class TypeOrmRepository extends UserRepositoryAbstract {
    constructor(@InjectRepository(User, TYPEORM_DATABASE) private readonly userRepository: Repository<User>,) {
        super();
    }

    findAll(page: number = 10, limit: number = 1): Promise<any[]> {
        return this.userRepository.find({ skip: (page - 1) * limit, take: limit })
    }

    findOne(username: string) {
        return this.userRepository.findOneBy({ name: username })
    }

    find(): Promise<any[]> {
        return this.userRepository.find()
    }

    async create(userObj: any): Promise<any> {
        return await this.userRepository.create(userObj)
    }
    update(userObj: any): Promise<any> {
        return this.userRepository.update({ id: userObj.id }, userObj)
    }
    delete(id: string): Promise<any> {
        return this.userRepository.delete(id)
    }

}